<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>reduce 数组去重</title>
</head>

<body>

</body>

</html>
<script>
    /**
     * 函数作为累加器，数组中的每个值（从左到右）开始缩减，最终计算为一个值
     *  total 初始值，或者计算结束后的返回值
     *  item 当前元素
     *  arr 当前元素所属的数组对象
     */
    var persons = [{
        id: 1,
        name: 'yong'
    }, {
        id: 1,
        name: 'sai'
    }]
    var obj = {}
    var newPersons = persons.reduce(function(total, item) {
        obj[item.id] ? '' : (obj[item.id] = total.push(item))
        return total
    }, [])
    console.log('oldPersons', persons)
    console.log('newPersons', newPersons)
</script>